package com.yiwei.controller;

import org.apache.commons.io.monitor.FileAlterationListenerAdaptor;
import org.apache.commons.io.monitor.FileAlterationMonitor;
import org.apache.commons.io.monitor.FileAlterationObserver;

import java.io.File;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.logging.Logger;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class YWFileMonitor extends FileAlterationListenerAdaptor {
    private Logger log = Logger.getLogger(YWFileMonitor.class.getName());
    private YWCloudPlatformUtils ywCloudPlatformUtils;
    private ConcurrentLinkedQueue<ImageFileInfoObject> imgIdQueue = new ConcurrentLinkedQueue<ImageFileInfoObject>();

    public YWFileMonitor() throws Exception {
        ywCloudPlatformUtils = new YWCloudPlatformUtils();
        FileAlterationObserver observer = new FileAlterationObserver(AssistantConfig.properties.getProperty("imageDir"));
        observer.addListener(this); //设置文件变化监听器
        //创建文件变化监听器
        FileAlterationMonitor monitor = new FileAlterationMonitor(2000, observer);
        // 开始监控
        monitor.start();
        log.info("文件监控服务就绪，"+AssistantConfig.properties.getProperty("imageDir"));
    }

    @Override
    public void onFileCreate(File file) {
        log.info(String.format("新增文件%s", file.getName()));
        long fSize = file.length();
        long sizeLimit = 0;
        try {
            String fileSize = AssistantConfig.properties.getProperty("fileSize");
            sizeLimit = Long.parseLong(fileSize);
        }catch (Exception e){
            e.printStackTrace();
        }
        if (fSize < sizeLimit){
            log.info(file.getName()+"文件小于"+sizeLimit);
            return;
        }
        //获取文件的后缀名 .jpg
//        String suffix = file.getName().substring(file.getName().lastIndexOf("."));
//        String[] suffixArr = AssistantConfig.properties.getProperty("fileSuffix").split(",");
//        for (String s : suffixArr){
//            if (suffix.length() > 0){
//                if (file.getName().toLowerCase().endsWith(s.toLowerCase())){
//                    log.info("后缀名符合过滤条件");
//                    new Thread(new FileUploaderThread(file)).start(); //启动一个线程来上传影像
//                    break;
//                }
//            }
//
//        }

        //匹配文件过滤正则表达式
        String fileRegex = AssistantConfig.properties.getProperty("fileRegex");
        Matcher matcher = Pattern.compile(fileRegex).matcher(file.getName());
        if (matcher.find()){
            log.info("后缀名符合过滤条件");
            new Thread(new FileUploaderThread(file)).start(); //启动一个线程来上传影像
        }

    }

//    @Override
//    public void onFileChange(File file) {
//        log.info(String.format("修改文件%s", file.getName()));
//    }
//
//    @Override
//    public void onFileDelete(File file) {
//        log.info(String.format("删除文件文件%s", file.getName()));
//    }

    public ConcurrentLinkedQueue<ImageFileInfoObject> getImgIdQueue() {
        return imgIdQueue;
    }


    class FileUploaderThread implements Runnable{
        private File uploadFile;

        public FileUploaderThread(File uploadFile){
            this.uploadFile = uploadFile;
        }

        @Override
        public void run() {
            log.info("上传线程启动，imageName:"+uploadFile.getName());
            String token = "";
            token = AssistantConfig.properties.getProperty("token");
            String img_id = ywCloudPlatformUtils.uploadFileAndGetImgId(token, uploadFile);
            if (img_id != null) {
                ImageFileInfoObject imageFileInfoObject = new ImageFileInfoObject(uploadFile.getName(), img_id);
                imgIdQueue.add(imageFileInfoObject);
                log.info("文件上传成功，img_id:"+img_id+", 队列长度"+imgIdQueue.size());
            }else {
                log.warning("文件上传失败");
            }
        }
    }
}
